<?php

namespace app\common\model\task;

use app\common\model\DiamondLog;
use app\common\model\GoldLog;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use app\common\model\scroll\Notify;
use app\common\model\User;
use Monolog\Handler\SwiftMailerHandler;
use think\Exception;
use think\Model;


class Userlog extends Model
{


    // 表名
    protected $table = 'task_userlog';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    public static function check($task, $user_id)
    {
        if ($task['date_limit'] != 'more') {
            $log = self::where(['user_id' => $user_id, 'task_id' => $task['id']])
                ->whereTime('createtime', $task['date_limit'])
                ->order("createtime DESC")->find();//是否有任务
        } else {
            $log = self::where(['user_id' => $user_id, 'task_id' => $task['id']])
                ->order("createtime DESC")->find();//是否有任务
        }

        if ($log) {
            if ($task['type'] == 1) {
                $status = $log['status'];
            } else {
                $model = self::where([
                    'user_id' => $user_id,
                    'task_id' => $task['id'],
                    'status' => 1
                ]);
                if ($task['date_limit'] != 'more') {
                    $model->whereTime('createtime', $task['date_limit']);
                }
                $num = $model->count();
                //条数限制
                if ($num >= $task['frequency_limit']) {
                    $status = 1;//总数已完成
                } else if ($log['status'] == 1) {
                    $status = -1;
                } else {
                    $status = $log['status'];
                }
            }
        } else {
            $status = -1;
        }

        return $status;
    }

    public static function award($task, $user_id, $nickname)
    {
        $log = self::where(['task_id' => $task['id'], 'user_id' => $user_id])->find();
        if (!$log) {
            throw new Exception("任务未领取");
        }

        if ($log['status'] != 0) {
            throw new Exception("当前任务状态有误");
        }

        if ($task['type'] == 3) {
            if ($log['is_audit'] != 1) {
                throw new Exception("当前任务审核未完成");
            }
        }
        $log->save(['status' => 1]);
        $user = User::get($user_id);
        $type_name = "";
        switch ($task['award_type']) {
            case 1:
                $model = new MoneyLog();
                $key = 'money';
                $type_name = "银币";
                break;
            case 2:
                $model = new GoldLog();
                $key = 'gold';
                $type_name = "金币";
                break;
            case 3:
                $model = new ScoreLog();
                $key = 'score';
                $type_name = "积分";
                break;
            case 4:
                $model = new DiamondLog();
                $key = 'diamond';
                $type_name = "钻石";
                break;
        }
        Notify::log("{$task['name']},获得{$task['amount']}{$type_name}", $user['nickname'], 0);

        $before = $user[$key];
        $after = function_exists('bcadd') ? bcadd($user[$key], $task['amount'], 2) : $user[$key] + $task['amount'];
        $user->save([$key => $after]);
        $model->insertGetId([
            'user_id' => $user_id, $key => $task['amount'],
            'before' => $before, 'after' => $after,
            'memo' => "任务奖励", 'type' => 8,
            'createtime' => time()
        ]);
        return true;
    }

    public static function get_task($task_id, $user_id, $status = 0, $is_audit = 2)
    {
        self::create([
            'user_id' => $user_id,
            'task_id' => $task_id,
            'status' => $status,
            'is_audit' => $is_audit
        ]);
        return true;
    }

    public function getStatusList()
    {
        return [0 => '待领取', 1 => '已完成', 2 => '未审'];
    }

    public function getAuditList()
    {
        return [0 => '待审核', 1 => '已审核', 2 => '无须审核'];
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function lists()
    {
        return $this->belongsTo('Lists', 'task_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
